//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdBlackScholesRebateEngine.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL::Processes;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine::CFdBlackScholesRebateEngine (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Microsoft::FSharp::Core::FSharpOption<UInt64>^ tGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ xGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ dampingSteps, Microsoft::FSharp::Core::FSharpOption<Double>^ theta, Microsoft::FSharp::Core::FSharpOption<Boolean>^ localVol, Microsoft::FSharp::Core::FSharpOption<Double>^ illegalLocalVolOverwrite) : CPricingEngine(CFdBlackScholesRebateEngine::typeid)
{
    CGeneralizedBlackScholesProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phFdBlackScholesRebateEngine = NULL;
#endif
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        QuantLib::Size _tGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (tGrid) ? (QuantLib::Size)ValueHelper::Convert (tGrid->Value) : 100); //4
        QuantLib::Size _xGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (xGrid) ? (QuantLib::Size)ValueHelper::Convert (xGrid->Value) : 100); //4
        QuantLib::Size _dampingSteps = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (dampingSteps) ? (QuantLib::Size)ValueHelper::Convert (dampingSteps->Value) : 0); //4
        QuantLib::Real _theta = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (theta) ? (QuantLib::Real)ValueHelper::Convert (theta->Value) : 0.5); //4
        bool _localVol = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (localVol) ? (bool)ValueHelper::Convert (localVol->Value) : false); //4
        QuantLib::Real _illegalLocalVolOverwrite = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (illegalLocalVolOverwrite) ? (QuantLib::Real)ValueHelper::Convert (illegalLocalVolOverwrite->Value) : -Null<QuantLib::Real>()); //4
        _ppFdBlackScholesRebateEngine = new boost::shared_ptr<QuantLib::FdBlackScholesRebateEngine> (new QuantLib::FdBlackScholesRebateEngine ( _process,  _tGrid,  _xGrid,  _dampingSteps,  _theta,  _localVol,  _illegalLocalVolOverwrite ));
        SetPricingEngine (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdBlackScholesRebateEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine::CFdBlackScholesRebateEngine (boost::shared_ptr<QuantLib::FdBlackScholesRebateEngine>& childNative, Object^ owner) : CPricingEngine(CFdBlackScholesRebateEngine::typeid)
{
#ifdef HANDLE
	_phFdBlackScholesRebateEngine = NULL;
#endif
	_ppFdBlackScholesRebateEngine = &childNative;
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdBlackScholesRebateEngine));
}
Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine::CFdBlackScholesRebateEngine (QuantLib::FdBlackScholesRebateEngine& childNative, Object^ owner) : CPricingEngine(CFdBlackScholesRebateEngine::typeid)
{
#ifdef HANDLE
	_phFdBlackScholesRebateEngine = NULL;
#endif
	_ppFdBlackScholesRebateEngine = new boost::shared_ptr<QuantLib::FdBlackScholesRebateEngine> (&childNative);
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdBlackScholesRebateEngine));
    _FdBlackScholesRebateEngineOwner = owner;
    _PricingEngineOwner = owner;
}

Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine::CFdBlackScholesRebateEngine (CFdBlackScholesRebateEngine^ copy) : CPricingEngine(CFdBlackScholesRebateEngine::typeid)
{
#ifdef HANDLE
	_phFdBlackScholesRebateEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdBlackScholesRebateEngine = new boost::shared_ptr<QuantLib::FdBlackScholesRebateEngine> (copy->GetShared());
        _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdBlackScholesRebateEngine));
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine::CFdBlackScholesRebateEngine (System::Type^ t) : CPricingEngine(CFdBlackScholesRebateEngine::typeid)
{
#ifdef HANDLE
	_phFdBlackScholesRebateEngine = NULL;
#endif
	if (!t->IsSubclassOf(CFdBlackScholesRebateEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine::CFdBlackScholesRebateEngine (QuantLib::Handle<QuantLib::FdBlackScholesRebateEngine>& childNative, Object^ owner)  : CPricingEngine(CFdBlackScholesRebateEngine::typeid)
{
	_phFdBlackScholesRebateEngine = &childNative;
	_ppFdBlackScholesRebateEngine = &static_cast<boost::shared_ptr<QuantLib::FdBlackScholesRebateEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdBlackScholesRebateEngine));
    _FdBlackScholesRebateEngineOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine::CFdBlackScholesRebateEngine (QuantLib::Handle<QuantLib::FdBlackScholesRebateEngine> childNative)  : CPricingEngine(CFdBlackScholesRebateEngine::typeid)
{
	_phFdBlackScholesRebateEngine = &childNative;
	_ppFdBlackScholesRebateEngine = &static_cast<boost::shared_ptr<QuantLib::FdBlackScholesRebateEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdBlackScholesRebateEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine::CFdBlackScholesRebateEngine (QuantLib::FdBlackScholesRebateEngine childNative)  : CPricingEngine(CFdBlackScholesRebateEngine::typeid)
{
#ifdef HANDLE
	_phFdBlackScholesRebateEngine = NULL;
#endif
	_ppFdBlackScholesRebateEngine = new boost::shared_ptr<QuantLib::FdBlackScholesRebateEngine> (new QuantLib::FdBlackScholesRebateEngine (childNative));
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdBlackScholesRebateEngine));
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine::~CFdBlackScholesRebateEngine ()
{
    if (_ppFdBlackScholesRebateEngine != NULL)
    {
	    delete _ppFdBlackScholesRebateEngine;
        _ppFdBlackScholesRebateEngine = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine::!CFdBlackScholesRebateEngine ()
{
    if (_ppFdBlackScholesRebateEngine != NULL)
    {
	    delete _ppFdBlackScholesRebateEngine;
    }
}
QuantLib::FdBlackScholesRebateEngine& Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine::GetReference ()
{
    if (_ppFdBlackScholesRebateEngine == NULL) throw gcnew NativeNullException ();
	return **_ppFdBlackScholesRebateEngine;
}
boost::shared_ptr<QuantLib::FdBlackScholesRebateEngine>& Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine::GetShared ()
{
    if (_ppFdBlackScholesRebateEngine == NULL) throw gcnew NativeNullException ();
	return *_ppFdBlackScholesRebateEngine;
}
QuantLib::FdBlackScholesRebateEngine* Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine::GetPointer ()
{
    if (_ppFdBlackScholesRebateEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppFdBlackScholesRebateEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdBlackScholesRebateEngine>& Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine::GetHandle ()
{
	if (_phFdBlackScholesRebateEngine == NULL)
	{
		_phFdBlackScholesRebateEngine = new Handle<QuantLib::FdBlackScholesRebateEngine> (*_ppFdBlackScholesRebateEngine);
	}
	return *_phFdBlackScholesRebateEngine;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine::HasNative () 
{
	return (_ppFdBlackScholesRebateEngine != NULL);
}

Cephei::QL::Experimental::Finitedifferences::IFdBlackScholesRebateEngine^ Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine::Calculate::get ()
{
    try
    {
    	(*_ppFdBlackScholesRebateEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdBlackScholesRebateEngine^ Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesRebateEngine_Factory::Create (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Microsoft::FSharp::Core::FSharpOption<UInt64>^ tGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ xGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ dampingSteps, Microsoft::FSharp::Core::FSharpOption<Double>^ theta, Microsoft::FSharp::Core::FSharpOption<Boolean>^ localVol, Microsoft::FSharp::Core::FSharpOption<Double>^ illegalLocalVolOverwrite)
{
    return gcnew CFdBlackScholesRebateEngine ( process,  tGrid,  xGrid,  dampingSteps,  theta,  localVol,  illegalLocalVolOverwrite);
}
